/**
 * 
 */
package com.sofast.dao;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.StringUtils;
import com.sofast.bean.SongerBean;
import com.sofast.common.DBConfig;

/**
 * @author alex
 *
 */
public class SongerDao {
	
	public Connection conn;
	
	public SongerDao() {
		conn = DBConfig.getConn();
	}
	
	public long insert(SongerBean songer) {
		long id = -1;
		try {
			String sql = "INSERT INTO songer(name,hot,namezone,summary) values ('"+songer.getName()+"',"+
							songer.getHot()+",'"+songer.getNameZone()+"','"+songer.getSummary()+"')";
			PreparedStatement sta = conn.prepareStatement(sql, new String[]{"id"});
			DatabaseMetaData dmd = conn.getMetaData();
			sta.executeUpdate();
			if(dmd.supportsGetGeneratedKeys()) {
				ResultSet res = sta.getGeneratedKeys();
				if(res.next()) {
					id = res.getLong(1);
				}
				res.close();
			}
			sta.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return id;
	} 
	
	public static void main(String[] args) {
		SongerDao sd = new SongerDao();
		
		SongerBean songer = new SongerBean();
		songer.setName("alex");
		songer.setHot(1000);
		songer.setNameZone("AA");
		songer.setSummary("good");
		
		System.out.println(sd.insert(songer));
	}
}
